package com.petmagics.learnhttp;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * HttpClient Post方式获取数据
 */
public class HttpClientPostFm extends Fragment {

    private EditText et;
    private TextView text;

    //google推荐使用HttpURLConnection 原则上要比 httpclient快
    HttpClient client;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_httpclient_post, container, false);
        et = (EditText) root.findViewById(R.id.editText);
        text = (TextView) root.findViewById(R.id.tw);
        client = new DefaultHttpClient();

        root.findViewById(R.id.btnReadData1111).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //android中使用10.0.2.2:8080做本地访问地址
                readNetDate("http://10.0.2.2:8080/test/httpclienttest", et.getText().toString());
            }
        });
        return root;
    }

    public void readNetDate(final String url, String params) {
        new AsyncTask<String, Void, String>() {

            @Override
            protected String doInBackground(String... strings) {
                String urlStr = strings[0];
                HttpPost post = new HttpPost(urlStr);
                try {
                    List<BasicNameValuePair> list = new ArrayList<>();
                    list.add(new BasicNameValuePair("name", strings[1]));
                    post.setEntity(new UrlEncodedFormEntity(list));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                try {
                    HttpResponse response = client.execute(post);
                    String value = EntityUtils.toString(response.getEntity());
                    return value;
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                text.setText(result);
            }
        }.execute(url,params);
    }
}
